﻿package work;

import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import com.h819.core.utils.CharSetUtils;

public final class Test {

	private CharSetUtils charSetUtils = new CharSetUtils();

	private void getHtml() {
		String code = "";
		String realcode = "";
		String name = "";

		System.out.println("Add watermarks and pagenumbers");

		File f = new File("D:\\ftpFiles\\2011-05-04_10-35-05");

		File[] list = f.listFiles();

		StringBuffer buf = new StringBuffer();

		int i = 0;

		for (File ff : list) {

			String temp = FilenameUtils.getBaseName(ff.getName());

			i++;

			code = StringUtils.substringBefore(temp, "_");
			realcode = code.replace('~', ' ').replace('!', '/');
			name = StringUtils.substringAfter(temp, "_");

			// String s1 = "<tr height=\"17\">\n<td>";
			// String s2 = "<a href=\"../../upload/milk/" + code
			// + "\" target=\"_blank\">" + realcode + "</a></td>";
			// String s3 = "<td ><a href=\"../../upload/milk/" + code
			// + "\" target=\"_blank\">" + name + "</a></td>";
			// String s4 = "<td><a href=\"../../upload/milk/" + code
			// + "\" target=\"_blank\">点击查看</a></td></tr>";

			String s1 = "<tr height=\"17\"><td  class=\"num\">" + i
					+ "</td>\n<td>";
			String s2 = "<a href=\"../../upload/milk/" + code + ".pdf"
					+ "\" target=\"_blank\">" + realcode + "</a></td>";
			String s3 = "<td >" + name + "</td>";
			String s4 = "</tr>";

			System.out.println("temp:" + temp);
			System.out.println("code:" + code);
			System.out.println("realcode:" + realcode);
			System.out.println("name:" + name);

			System.out.println(s1 + s2 + "\n" + s3 + "\n" + s4);

			buf.append(s1 + s2 + "\n" + s3 + "\n" + s4);

		}

		try {

			FileUtils.writeStringToFile(new File("d:\\f.txt"), buf.toString());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void removeHref() {

		File f = new File(
				"F:\\webdesign\\capital-std\\special_topic\\foodadditive\\2011foodadditive.html");

		StringBuffer buf = new StringBuffer();

		try {
			List<String> li = FileUtils.readLines(f);

			for (String s : li) {

				if (s.trim().endsWith("点击标准号，可以查看标准正文。"))
					continue;

				if (StringUtils.contains(s, ".PDF")) {
					String temp1 = "<a"
							+ StringUtils.substringBetween(s, "<a", ">") + ">";
					String temp2 = StringUtils.remove(s, temp1);
					String temp3 = StringUtils.remove(temp2, "</a>");
					String temp0 = temp3;
					buf.append(temp0 + "\n");
				} else {

					buf.append(s + "\n");
				}
			}

			try {

				FileUtils.writeStringToFile(new File("d:\\f2.txt"),
						buf.toString());

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void fileReadTest() {

		// 包含公司名称的集合
		List<String> company = new ArrayList<String>();

		File f = new File("d:\\1.txt");

		System.out.println(charSetUtils.getFileCharacterEncoding(f
				.getAbsolutePath()));

		try {
			company = FileUtils.readLines(f, "GBK");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		System.out.println(company.size());

		for (String companyName : company) {
			// like ; not like

			System.out.println(companyName);

			if (StringUtils.isEmpty(companyName) || companyName.equals(""))
				System.out.println(companyName + "is break.");

		}

	}

	private void fileCharTest() {

		charSetUtils.changeFileCharacterEncoding(new File("d:\\55.txt"),
				"windows-1252");

	}

	public String getFileCharacterEncoding(String filePath) {

		return charSetUtils.getFileCharacterEncoding(filePath);
	}

	private int countMatch() {

		//if(StringUtils.countMatches(stdFileName.replaceAll("\\", "/"), "/")>1){
		
		String temp = "CEN/CENELEC\\EN60335-2-75Y2004(A2).pdf";
	//	temp = StringUtils.
		
		
		temp = temp.replace("\\", "/");
		return StringUtils.countMatches(temp, "/");

	}

	public static void main(String[] args) {

		Test t = new Test();

		// t.removeHref();

		// t.fileReadTest();

		// t.fileCharTest();

		//System.out.println(t.getFileCharacterEncoding("d:\\1.txt"));
		
		System.out.println(t.countMatch());

	}
}
